<?php
/**
 * session 入库 memcache
 *
 */

namespace Think\Session\Driver;

class Memcached
{

    /**
     * Session有效时间
     */
   protected $lifeTime      = 3600;


    /**
     * 数据库句柄
     */
   protected $hander  = array(); 

    /**
     * 打开Session 
     * @access public 
     * @param string $savePath 
     * @param mixed $sessName  
     */
    public function open($savePath, $sessName)
    {
        $this->sessionName = $sessName;

        if(isset($this->hander['expire']) && !empty($this->hander['expire'])){
            $this->lifeTime = $this->hander['expire'];
        }

        $this->hander = S(array_merge(C('memcached_pool.memcached'), array('type' => 'memcached')));

       return true;
    } 

    /**
     * 关闭Session 
     * @access public 
     */
   public function close()
   {
       $this->gc(ini_get('session.gc_maxlifetime'));

       return true;
   }

    /**
     * 读取Session 
     * @access public 
     * @param string $sessID 
     */
   public function read($sessID)
   {
       $res = '';
       $res = $this->hander->get($this->sessionName  .$sessID);

       return $res;
   } 

    /**
     * 写入Session 
     * @access public 
     * @param string $sessID  // verify Id: PHPSESSIDracadduh2fvdpn48j2db4mqvd1
     * @param String $sessData  
     */
   public function write($sessID,$sessData)
   {

       $expire = time() + $this->lifeTime;
       $res = $this->hander->set($this->sessionName . $sessID, $sessData, $expire);

       if($res)
           return true; 
       return false; 
   } 

    /**
     * 删除Session 
     * @access public 
     * @param string $sessID 
     */
   public function destroy($sessID)
   {

       return $this->hander->delete($this->sessionName . $sessID);
   } 

    /**
     * Session 垃圾回收
     * @access public 
     * @param string $sessMaxLifeTime 
     */
   public function gc($sessMaxLifeTime)
   {

       return true;
   } 

}

